package org.poem.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.poem.IdsVo;
import org.poem.datasource.DataAndParametersVo;
import org.poem.result.CommonResult;
import org.poem.result.PageableResult;
import org.poem.service.TTaskDetailService;
import org.poem.task.TTaskDetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/v1/taskDetail")
@Api(tags = {
        "任务管理"
})
public class TaskDetailController {

    private static final Logger logger = LoggerFactory.getLogger(TaskDetailController.class);

    @Autowired
    private TTaskDetailService tTaskDetailService;

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "1_可用记录", notes = "可用记录")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 404, message = "请求路径没有找到")})
    @PostMapping(value = "/availability/{id}")
    public CommonResult<String> availability(@PathVariable("id") Long id){
        tTaskDetailService.availability(id);
        return new CommonResult<>();
    }
    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "2_不可用记录", notes = "可用记录")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 404, message = "请求路径没有找到")})
    @PostMapping(value = "/unavailable/{id}")
    public CommonResult<String> unavailable(@PathVariable("id") Long id){
        tTaskDetailService.unavailable(id);
        return new CommonResult<>();
    }

    /**
     *
     * @param pageSize
     * @param pageNumber
     * @return
     */
    @ApiOperation(value = "0201_展示数据源管理-带有分页", notes = "展示数据源管理")
    @ApiResponses
            ({
                    @ApiResponse(code = 400, message = "请求参数没有填好"),
                    @ApiResponse(code = 404, message = "请求路径没有找到")
            })
    @GetMapping("/getTaskDetailPage/{size}/{number}")
    @ResponseBody
    public CommonResult<PageableResult<TTaskDetailVO>> getTaskDetailPage( @PathVariable("size") Integer pageSize,
                                                            @PathVariable("number") Integer pageNumber){
        logger.info("TDataSourceController:getTaskDetailPage  [pageSize]:" + pageSize + ", [pageNumber]: " + pageNumber);
        PageableResult<TTaskDetailVO> dataSourceList = tTaskDetailService.getTaskDetailPage( pageSize, pageNumber);
        return new CommonResult<>(dataSourceList);
    }
    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询数据列表", notes = "可用记录")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 404, message = "请求路径没有找到")})
    @GetMapping(value = "/getTTaskDetailById/{id}")
    public CommonResult<TTaskDetailVO> getTTaskDetailById(@PathVariable("id") Long id){
        TTaskDetailVO tTaskDetailVO = tTaskDetailService.getTTaskDetailById(id);
        return new CommonResult<>(tTaskDetailVO);
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "新增或者是修改任务", notes = "可用记录")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 404, message = "请求路径没有找到")})
    @PostMapping(value = "/saveOrUpdateTaskDetail")
    public CommonResult<String> saveOrUpdateTaskDetail(@RequestBody TTaskDetailVO tTaskDetailVO){
        logger.info("saveOrUpdateTaskDetail:{}", JSONObject.toJSONString(tTaskDetailVO));
        String msg =  tTaskDetailService.saveOrUpdateTaskDetail(tTaskDetailVO);
        return new CommonResult<>(msg);
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除数据", notes = "可用记录")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 404, message = "请求路径没有找到")})
    @PostMapping(value = "/deleteTask")
    public CommonResult<String> deleteTask(@RequestBody IdsVo idsVo){
        String msg =  tTaskDetailService.deleteTask(idsVo);
        return new CommonResult<>(msg);
    }

    /**
     * 10-数据源测试连接
     *
     * @param dataSourceVo
     * @return
     */
    @ApiOperation(value = "3114_获取数据库信息", notes = "获取数据库信息")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没有填好"),
            @ApiResponse(code = 404, message = "请求路径没有找到")
    })
    @PostMapping("/getSchema")
    @ResponseBody
    public CommonResult<List<String>> getSchema(@RequestBody DataAndParametersVo dataSourceVo) {
        logger.info("CDataSourceController:getSchema:[dataSourceVo]:" + JSONObject.toJSONString(dataSourceVo));
        List<String> schema =  this.tTaskDetailService.getSchema(dataSourceVo);
        return new CommonResult<>(0, schema, "链接正确.");
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "开始全量同步", notes = "可用记录")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 404, message = "请求路径没有找到")})
    @PostMapping(value = "/startCompleteStartTask/{taskId}")
    public CommonResult<String> startCompleteStartTask(@PathVariable("taskId") Long taskId){
       tTaskDetailService.startCompleteStartTask(taskId);
        return new CommonResult<>(null);
    }

}
